﻿using System;
using System.Drawing;
using System.Globalization;
using ContractManagementSystem.BusinessLogic;

namespace ContractManagementSystem
{
    public partial class ViewProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewPanel.Visible = true;
                ChangePanel.Visible = false;
                GenderDropDownList.Items.Add("Male");
                GenderDropDownList.Items.Add("Female");
                GenderDropDownList.Items.Add("Other");
                FetchAll();
            }
        }

        private void FetchAll()
        {
            UserNameLabel.Text = Session["Name"].ToString();
            var dataTable = UserBusiness.FetchProfileByUserName(Session["Name"].ToString());

            NameTextBox.Text = dataTable.Rows[0]["FullName"].ToString();
            BirthDateTextBox.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", dataTable.Rows[0]["BirthDate"]);
            GenderDropDownList.SelectedIndex = int.Parse(dataTable.Rows[0]["Gender"].ToString(), CultureInfo.CurrentCulture);
            EmailTextBox.Text = dataTable.Rows[0]["Email"].ToString();
            try
            {
                PhoneNumberTextBox.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
            }
            catch (ArgumentException)
            {
                PhoneNumberTextBox.Text = dataTable.Rows[0]["PhoneNumber1"].ToString();
            }
        }

        protected void ChangePassLinkButton_OnClick(object sender, EventArgs e)
        {
            ViewPanel.Visible = false;
            ChangePanel.Visible = true;
        }

        protected void BackButton_OnClick(object sender, EventArgs e)
        {
            ViewPanel.Visible = true;
            ChangePanel.Visible = false;
        }

        protected void ChangePassButton_OnClick(object sender, EventArgs e)
        {
            if (OldPasswordTextBox.Text == UserBusiness.FetchPassword(Session["Name"].ToString()))
            {
                if (UserBusiness.UpdatePassword(NewPasswordTextBox.Text, Session["Name"].ToString()))
                {
                    MessLabel.Text = "Change success !";
                    MessLabel.ForeColor = Color.Teal;
                }
            }
            else
            {
                MessLabel.Text = "Change fail !";
                MessLabel.ForeColor = Color.Red;
            }
        }
    }
}
